package com.huaju.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huaju.enity.OpenIdBean;
import com.wx.tools.HttpUtil;

/**
 * 微信公众号支付
 * 
 * @author EasonYang
 *
 */
@Controller
public class WebchatPayController extends BaseController {

	private final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=wx0f6cd216117a7e85&secret=c473b1122f9302b000d0dea8d6447eb8&grant_type=authorization_code&js_code=";
//	private final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=wx0fb5f73a8c51d80a&secret=269dc2d1a4a40cc07fbc010689e8573f&grant_type=authorization_code&js_code=";
//	private final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=wx0f6cd216117a7e85&secret=c473b1122f9302b000d0dea8d6447eb8&grant_type=authorization_code&js_code=";


	/***
	 * 课程详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/App/WxLogin.action")
	@ResponseBody
	public Map addressUs(
			@RequestParam(value = "code", required = false) String code,
			ModelMap map) {
			this.result = new HashMap();
			//微信auth认证通过
			if(code!=null&&!code.equals("")){
					//code 转换 openid  uid
				OpenIdBean  ob = new OpenIdBean();	
					try {
						System.out.println(OPENID_URL);
						String   jsonStr = HttpUtil.getHtmlString(OPENID_URL+code);
						System.out.println("oauthbeanStr:"+jsonStr);
						Gson gson = new Gson();
						java.lang.reflect.Type type = new TypeToken<OpenIdBean>() {
						}.getType();
						ob = gson.fromJson(jsonStr, type);
						

						if(ob.getSession_key()!=null&&!ob.getOpenid().equals("")){
							//获取成功！
							String  openId = ob.getOpenid();	
							System.out.println("openId:"+openId);
							map.put("openId", ob.getOpenid());
							System.out.println(ob);
							this.status = 200;
							this.result.put("data", ob);
							this.message = "";
						}else{
							this.status = 201;
							this.result.put("data", "");
							this.message = "信息确认失败！";
						}
					} catch (Exception e) {
						e.printStackTrace();
						this.status = 202;
						this.result.put("data", "");
						this.message = "信息确认失败！";
					}	
					
					
			}else{
				this.status = 400;
				this.result.put("data", "");
				this.message = "信息确认失败！";
			}
			

			return getResult();
		
	}
}


